// 可以使用的网址
var sites = ["item.taobao.com", "item.jd.com", "item.yhd.com", "product.suning.com", "www.suning.com", "www.gome.com.cn", "www.vip.com", "detail.tmall.com", "detail.ju.taobao.com"];
var b = {
	site: null,
	target: null,
	init: function() {
		this.detectSite();
		if(this.site != '') {
			this.target = document.createElement("a");
			this.target.href = "http://www.baidu.com?s="+ document.URL;
			this.target.id = "buy_behalf_btn";
			this.target.target = "_blank";
			var img = document.createElement("img");
			img.src = "http://download.easyicon.net/ico/241/16/";
			img.style.verticalAlign = "bottom";
			img.style.marginBottom = "10px";
			img.style.marginRight = "5px";
			this.target.appendChild(img);
			var text = document.createTextNode("找人代购");
			this.target.appendChild(text);
			this.insertBuyLink();
		}
	},
	detectSite: function() {
		var url = document.URL;
		var urlRegex = /http(s)?:\/\/([^/]+)/;
		var host = urlRegex.exec(url)[2];
		var index = -1;
		if((index = sites.indexOf(host)) != -1) {
			this.site = host;
		}	
	},
	insertBuyLink: function() {
		var obj;
		switch(this.site) {
			case sites[0]:
				obj = document.getElementById("J_juValid");
				break;
			case sites[1]:
				obj = document.getElementById("choose-btn-append");
				break;
			case sites[2]:
				obj = document.getElementById("BtnArea");
				break;
			case sites[3]:
			case sites[4]:
				obj = document.getElementById("by_cart");
				break;
			case sites[5]:
				obj = document.getElementById("btnLink");
				break;
			case sites[6]:
				obj = document.getElementsByClassName("pi_price_box");
				break;
			case sites[7]:
				obj = document.getElementsByClassName("tm-fcs-panel");
				break;
			case sites[8]:
				obj = document.getElementsByClassName("operation");
				break;
		}
		if(obj.length) {
			obj[0].insertBefore(this.target, null); 
		} else {
			obj.insertBefore(this.target, null); 
		}
	}
}
b.init(); 
